#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define FTOK_PATH	"/dev/zero"
#define FTOK_PROJID	22

typedef struct st_student
{
	char	name[64];
	int	age;
}t_student;


int main (int argc,char **argv)
{
	key_t		key;
	int		shmid;
	t_student	*student;
	int		i;
	const char *path = "/dev/zero";
	
	
	if( (key=ftok(path,22)) < 0 )
	{
		printf("ftok() failure: %s\n",strerror(errno));
		return -1;
	}
	shmid = shmget(key,sizeof(t_student),IPC_CREAT|0666);
	if(shmid < 0)
	{
		printf("shmget() create shared memory failure: %s\n",strerror(errno));
		return -2;
	}

	student = shmat(shmid,NULL,0);
	if((void *)-1 == student)//=-1出错，强制类型转换
	{
		printf("shmat() alloc shared memory failure: %s\n",strerror(errno));
		return -2;
	}
	strncpy(student->name,"zhangsan",sizeof(student->name));
	student->age = 18;

	for(i=0;i<4;i++)
	{
		student->age++;
		printf("Student '%s' age [%d]\n",student->name,student->age);
		sleep(1);
	}

	shmdt(student);
	
	shmctl(shmid,IPC_RMID,NULL);

	return 0;
}


